/**
  ******************************************************************************
  * File Name          : ethtsk.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "ethernet.h"

TaskHandle_t xEthernet_Thd = NULL;
StaticTask_t xEthernet_Tcb;
StackType_t  xEthernet_Stk[ETHERNET_STACK_SIZE];

static Ethernet_SendQueStru tosend;

void vEthernet_Task(void * pvParameters)
{
  uint8_t isrmsg;
  Ethernet_CtrlQueStru ctrl;
  QueueSetMemberHandle_t activated_qhd;

  ethos_wizchip_initialize();

  while(1)
  {
    activated_qhd = xQueueSelectFromSet(ethernet_qshd, portMAX_DELAY);
    if(activated_qhd == ethernet_isr_qhd)         /* 中断消息 */
    {
      if(xQueueReceive(activated_qhd, &isrmsg, 0) !=NULL)
      {
        ethos_wizchip_getinterrupt();
      }
    }
    else if(activated_qhd == ethernet_send_qhd)   /* 发送消息 */
    {
      if(xQueueReceive(activated_qhd, &tosend, 0) !=NULL)
      {
        if(tosend.type == 1){
          ethos_send(&tosend);
        }else{
          ethos_sendto(&tosend);
        }
      }
    }
    else if(activated_qhd == ethernet_ctrl_qhd)   /* 控制消息 */
    {
      if(xQueueReceive(activated_qhd, &ctrl, 0) !=NULL)
      {
        switch(ctrl.type)
        {
          case ETH_CRTL_SOCKET:
            ethos_ctrl_socket(&ctrl);
            break;

          case ETH_CRTL_CLOSE:
            ethos_ctrl_close(&ctrl);
            break;

          case ETH_CRTL_LISTEN:
            ethos_ctrl_listen(&ctrl);
            break;

          case ETH_CRTL_CONNECT:
            ethos_ctrl_connect(&ctrl);
            break;

          case ETH_CRTL_DISCONNECT:
            ethos_ctrl_disconnect(&ctrl);
            break;
          
          case ETH_CRTL_GETSOCKETSR:
            ethos_ctrl_getsocketsr(&ctrl);
            break;
        }
      }
    }
    else
    {
      /* nothing */
    }
  }
}
